#include "mat_ui_achievement.h"

#include "gameflow/ui_popup.h"
#include "application/game_application.h"
#include "application/game_rms.h"



MATUIAchievement::MATUIAchievement(gameflow::UIPopup* pPopup)  : 
									MATUIPopupAnimator(pPopup)
{
	math::Vec2 Pos;
	GameRMS* pRMS = NULL;

	if(g_pApp)
		pRMS = g_pApp->GetRMS();

	
	if(m_pPopup)
	{
		m_pAnimateWidget = m_pPopup->GetWidget("frame");
		MATUIPopupAnimatorMgr::Instance().AddPopupAnimator(MATUIPopupAnimatorMgr::POPUP_NAMES_ACHIEVEMENT,this);
		m_pPopup->SetWidgetFunctor("Button_Close",UI_DELEGATE(MATUIAchievement, OnButtonClose));
		

		char Buffer[32];
		for(int i = 0 ; i < RMS_ACH_C; i++)
		{
			sprintf(Buffer, "icon_%i", i+1);
			m_pIcons[i] = m_pPopup->GetWidget(Buffer);
			if(m_pIcons[i])
			{
				if(pRMS && pRMS->GetInt(i+1) == 0)				
				{
					//m_pIcons[i]->SetOffImage("ui_img/questionmark.png");				
				}
				else
				{				
					sprintf(Buffer, "ui_img/icon_%i.png", i+1);
					m_pIcons[i]->SetOffImage(Buffer);
				}
				
				if(i ==0 ) 
				{
					//Pos = m_pIcons[i]->GetPosition();				
				}
				else
				{
					Pos += math::Vec2(0,m_pIcons[i]->GetSize().y);
					//m_pIcons[i]->SetPositionRelativeToParent(Pos);
				}				

			}

			
			sprintf(Buffer, "text_%i", i+1);
			m_pText[i] = m_pPopup->GetWidget(Buffer);
			
			if(m_pText[i])
			{
				sprintf(Buffer, "ACHIEVEMENT_TEXT_%i", i+1);
				m_pText[i]->SetTextA(Buffer);
				//m_pText[i]->SetPositionRelativeToParent(math::Vec2(0,Pos.y) );
				if(pRMS && pRMS->GetInt(i+1) == 0)
					m_pText[i]->SetTextColor(0.5f,0.5f,0.5f,1.0f);
				else
					m_pText[i]->SetTextColor(1.0f,1.0f,1.0f,1.0f);
			}


			sprintf(Buffer, "desc_%i", i+1);
			m_pDesc[i] = m_pPopup->GetWidget(Buffer);
			if(m_pDesc[i])
			{
				sprintf(Buffer, "ACHIEVEMENT_DESC_%i", i+1);
				m_pDesc[i]->SetTextA(Buffer);	
				//m_pDesc[i]->SetPositionRelativeToParent(math::Vec2(0,Pos.y + 0.025f) );

				if(pRMS && pRMS->GetInt(i+1) == 0)
					m_pDesc[i]->SetTextColor(0.5f,0.5f,0.5f,1.0f);
				else
					m_pDesc[i]->SetTextColor(1.0f,1.0f,1.0f,1.0f);
			}
			
		}

	}
	m_Type = ANIMATION_SLIDEFROMTOP;

}

void		MATUIAchievement::BuildUI()
{
	
}

MATPopupAchievement::MATPopupAchievement() : gameflow::UIPopup("ui/achievement.ui","")
{

}

void		MATPopupAchievement::Tick(float dt)
{

}

void		MATUIAchievement::Show()
{
	HideOffScreens();
	MATUIPopupAnimator::Show();
}

void		MATUIAchievement::HideOffScreens()
{	
	for(int i = 0 ; i < RMS_ACH_C; i++)
	{
		if(m_pIcons[i] && m_pIcons[i]->GetPosition().y > 1.0f)
		{
			m_pIcons[i] ->SetIsVisible(false);
		}
		if(m_pText[i] && m_pText[i]->GetPosition().y > 1.0f)
		{
			m_pText[i] ->SetIsVisible(false);
		}

		if(m_pDesc[i] && m_pDesc[i]->GetPosition().y > 1.0f)
		{
			m_pDesc[i] ->SetIsVisible(false);
		}

	}
}

void DELEGATE_CALL	MATUIAchievement::OnButtonClose(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	MATUIAnimator::Instance().Add(m_pAnimateWidget,MATAnimatedUI::ANIMATE_STYLE_HIDE_TOTOP,0, this);
}

void		MATUIAchievement::OnAnimatedUIFinish()
{
	if(m_pPopup)
		m_pPopup->Hide(NULL);
}

int		MATUIAchievement::CalculateCount()
{
	GameRMS* pRMS = NULL;

	if(g_pApp)
		pRMS = g_pApp->GetRMS();

	int AchievementCount  = 0;
	
	
	for(int i = 0 ; i < RMS_ACH_C; i++)
	{
		if(pRMS && pRMS->GetInt(i+1) == 1)				
		{
			AchievementCount++;
		}		
	}

	return AchievementCount;
}

void		MATUIAchievement::OnMouseButtonDown(const math::Vec2& Pos)
{
	if(m_pPopup && !m_pPopup->IsActive())
		return;

	m_PreviousMousePos = Pos;
}

